/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.testdesign;

import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_dataobject;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.orm.refinedclasses.DataObjectRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;
import org.testis.utils.parametermodel.ParameterModel;
import org.testis.utils.parametermodel.ParameterTable;

/**
 * 
 * @author nurhat
 */
public class DataObjectDataHandler extends TestPartDataHandler {

    @Override
    public TestDesignTreeNode operationAdd(HashMap<String, Object> parameters,
	    TestDesignTreeNode parentNode, int childIndex) {
	TestDesignTreeNode newNode = null;
	String name = (String) parameters.get("name");
	ParameterTable parameterTableModel = (ParameterTable) parameters
		.get("parameterTableModel");
	String modulType = (String) parameters.get("modulType");
	String expectedResultType = (String) parameters
		.get("expectedResultType");
	String owner = (String) parameters.get("owner");
	LinkedList<String> labels = (LinkedList<String>) parameters
		.get("labels");

	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		ParameterModel[] params = parameterTableModel.getAllItems();
		T_test_dataobject dataObj = new T_test_dataobject();
		T_test_designtree obj = new T_test_designtree();
		obj.save();
		TestDesignTreeRefined designTree = TestDesignTreeRefined
			.getDesignTree(obj);
		dataObj.setName(name);
		dataObj.setModulTypeId(TestDesign
			.getModuleTypeIdByName(modulType));
		dataObj.save();
		TestDesign.saveLabels(labels, dataObj);
		designTree.getTreeReference().setParent(
			parentNode.getReference().getTreeReference());
		designTree.getTreeReference().setElementId(dataObj.getId());
		designTree.getTreeReference().setTypeId(
			TestDesign.getDesignPartTypeIdByName("DataObject"));
		designTree.getTreeReference().save();
		if (owner != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), null);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.ThreadPoolSize.name(), null);
		designTree.setParameterValue(
			TestDesignTreeParameters.Timeout.name(), null);
		if (expectedResultType != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ExpectedResultTypeId
				    .name(), TestDesign.m_dataObjectResultTypes
				    .get(expectedResultType).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ExpectedResultTypeId
				    .name(), null);
		}
		designTree
			.setParameterValue(
				TestDesignTreeParameters.SuccessPercentage
					.name(), null);
		designTree.getTreeReference().save();

		DataObjectRefined result = new DataObjectRefined(dataObj);
		for (int i = 0; i < params.length; i++) {
		    result.addParameter(params[i].getParameter().trim(),
			    params[i].getValue().trim(), i);
		}
		designTree.getTreeReference().save();
		int place = TestDesign.setRankOfAdd(parentNode,
			designTree.getTreeReference(), childIndex);
		designTree.getTreeReference().save();
		t.commit();
		newNode = TestDesignTreeNode.getNode(designTree, result);
		if (place == -1) {
		    parentNode.addChild(newNode);
		} else {
		    parentNode.addChild(place, newNode);
		}
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
		newNode = null;
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	    newNode = null;
	}
	return newNode;
    }

    @Override
    public boolean operationSave(HashMap<String, Object> parameters,
	    TestDesignTreeNode node) {
	String name = (String) parameters.get("name");
	ParameterTable parameterTableModel = (ParameterTable) parameters
		.get("parameterTableModel");
	String expectedResultType = (String) parameters
		.get("expectedResultType");
	String owner = (String) parameters.get("owner");
	LinkedList<String> labels = (LinkedList<String>) parameters
		.get("labels");

	boolean result = false;
	DataObjectRefined dataObject = (DataObjectRefined) node.getObject();
	TestDesignTreeRefined designTree = node.getReference();
	T_test_dataobject dataObj;
	try {
	    TestisDbPersistentManager.instance().disposePersistentManager();
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		ParameterModel[] params = parameterTableModel.getAllItems();

		dataObject.deleteAllParameters();
		for (int i = 0; i < params.length; i++) {
		    dataObject.addParameter(params[i].getParameter().trim(),
			    params[i].getValue().trim(), i);
		}
		dataObj = dataObject.getReference();
		dataObj.setName(name);
		dataObj.save();
		TestDesign.saveLabels(labels, dataObj);
		if (owner != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), null);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.ThreadPoolSize.name(), null);
		designTree.setParameterValue(
			TestDesignTreeParameters.Timeout.name(), null);
		if (expectedResultType != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ExpectedResultTypeId
				    .name(), TestDesign.m_dataObjectResultTypes
				    .get(expectedResultType).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ExpectedResultTypeId
				    .name(), null);
		}
		designTree
			.setParameterValue(
				TestDesignTreeParameters.SuccessPercentage
					.name(), null);

		node.getReference().getTreeReference().save();
		t.commit();
		TestDesignTreeRefined[] clones = node.getClones();
		for (int i = 0; i < clones.length; i++) {
		    TestDesignTreeNode.getNode(clones[i]).setObject(dataObject);
		}
		result = true;

	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
	return result;
    }
}
